package by.jabbby.ui.act;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.RemoteException;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;
import by.jabbby.JabbbyException;
import by.jabbby.Logger;
import by.jabbby.dataprovider.ApplicationPreference;
import by.jabbby.dataprovider.ChatEntry;
import by.jabbby.dataprovider.ChatProvider;
import by.jabbby.service.JabberService;
import by.jabbby.service.JabberServiceProxy;
import by.jabbby.service.Status;

public class Primary extends TitleBar {
	public static final CharSequence DATE_FORMAT = "MM/dd/yy hh:mm";
	// JabberServiceProxy js;
	private BroadcastReceiver statusReciver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context paramContext, Intent paramIntent) {
			if (paramIntent.getAction().equals(JabberService.ACTION_CHANGE_STATUS)) {
				try {
					if (jabberProxy.getService().isLoggin()) {
						Primary.this.startActivity(new Intent(Primary.this.getApplicationContext(), Roster.class));
					}
				} catch (Exception e) {
					Logger.e(e);
				}
			}

		}
	};

	/*
	 * protected class BR extends BroadcastReceiver {
	 * 
	 * @Override public void onReceive(Context arg0, Intent intent) { if
	 * (JabberService.ACTION_CHANGE_STATUS.equals(intent.getAction())) { try {
	 * String statusStr = jabberProxy.getService() .getCurrentStatus(); Status
	 * status = Status.valueOf(statusStr); String appName =
	 * Primary.this.getString(R.string.app_name); switch (status) { case OnLine:
	 * Primary.this.setTitleColor(Color.GREEN); String tmp = appName + " " +
	 * status.getLocalization(Primary.this); Primary.this.setTitle(tmp); break;
	 * case OfLine: Primary.this.setTitleColor(Color.RED);
	 * Primary.this.setTitle(appName + " " +
	 * status.getLocalization(Primary.this)); break; }
	 * 
	 * } catch (RemoteException e) { // TODO Auto-generated catch block
	 * Logger.e(e); } catch (JabbbyException e) { // TODO Auto-generated catch
	 * blockLogger.e(e); }
	 * 
	 * }
	 * 
	 * }
	 * 
	 * }
	 */
	// private BR serviceReciver = null;
	// private JabberServiceProxy jabberProxy;
	// private MainMenuUtil menuUtil;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		try {
			super.onCreate(savedInstanceState);

			setContentView(R.layout.main);
			this.initTitleBar();
			this.getLeftTextView().setText(R.string.app_name);
			

			// boolean exit = savedInstanceState.getBoolean("exit");
			// if(exit){
			// this.finish();
			// }

			// registerRecivers();

			// getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
			// R.layout.titlebar);
			// jabberProxy = JabberServiceProxy.getInstance(this);
			// menuUtil = new MainMenuUtil(this, jabberProxy);
			// setContentView(R.layout.main);
			// .initTitleBar();
			// /

			// testChatProvider();
			// testresdChatProvider();
		} catch (Exception e) {
			Logger.e(e);
		}

	}

	private void testChatProvider() {
		ChatProvider cp = new ChatProvider("test jid", this.getApplicationContext());
		by.jabbby.dataprovider.ChatEntry ce = new ChatEntry("Test mesage", new Date(java.lang.System.currentTimeMillis()), "from", "to");
		cp.addEntry(ce);
	}

	private void testresdChatProvider() {
		ChatProvider cp = new ChatProvider("to", this.getApplicationContext());
		List<ChatEntry> res = cp.getEntries("to", "from");
		res.size();

	}

	@Override
	protected void onStart() {
		super.onStart();
		registerReciveirs();

		try {
			if (jabberProxy.isServiceBinded()) {
				
				if (jabberProxy.getService().isLoggin()) {
					Primary.this.startActivity(new Intent(Primary.this.getApplicationContext(), Roster.class));
				}
			}
		} catch (Exception e) {
			
			Logger.e(e);
		}
	}

	private void registerReciveirs() {
		IntentFilter intent = new IntentFilter(JabberService.ACTION_CHANGE_STATUS);
		this.registerReceiver(statusReciver, intent);
	}

	private void unregisterReceivers() {
		this.unregisterReceiver(statusReciver);
	}

	public void onJabberServiceBinded() {
		super.onJabberServiceBinded();
		try {
			
			if (jabberProxy.getService().isLoggin()) {
				Primary.this.startActivity(new Intent(Primary.this.getApplicationContext(), Roster.class));
			}else{
				if(ApplicationPreference.isAutoConnect(this.getApplicationContext())){
					jabberProxy.getService().changeStatus(Status.OnLine.name());
				}
			}
		} catch (Exception e) {			
			Logger.e(e);
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (jabberProxy != null) {
			jabberProxy.removeBinderListener(this);
		}

	}

	@Override
	protected void onPause() {
		super.onPause();
		unregisterReceivers();
	}

	@Override
	protected void onRestart() {
		// unRegisterRecivers();
		// registerRecivers();
		super.onRestart();
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		// registerRecivers();
		super.onRestoreInstanceState(savedInstanceState);
	}

	@Override
	protected void onResume() {

		super.onResume();
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {

	unVisibleMenuItems.clear();
		try {
/*			if (jabberProxy.isServiceBinded()) {
				if (!jabberProxy.getService().isLoggin()) {
					setVisibleMenuItemsOnly(R.id.mmi_account, R.id.mmi_status);
				}

			} else {
				for (Integer itemId : MainMenuUtil.mainMenuItems) {
					unVisibleMenuItems.add(itemId);
				}

			}
			*/
		} catch (Exception e) {
			Logger.e(e);
		}
		return 		super.onPrepareOptionsMenu(menu);
	}

}